package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class Yard extends Application{
    Magic m = new Magic();
    public static int ROW = 50;
    public static int COL = 50;
    public  static int BLOCK_SIZE = 10;
    Canvas canvas = new Canvas(COL*BLOCK_SIZE,ROW*BLOCK_SIZE);
    static Yard y =new Yard();
    public static  Snake s = new Snake(y);
    public boolean flag = true;
    public int score = 0;
    public static Food f = new Food();
    //键盘监听方法
    public void initKeyListener(Scene scene) {
        scene.setOnKeyPressed(e->{
            KeyCode key=e.getCode();
            switch(key){
                case LEFT:
                    if(s.head.dir==Direction.RIGHT)
                        break;
                    s.head.dir = Direction.LEFT;
                    break;
                case RIGHT:
                    if(s.head.dir==Direction.LEFT)
                        break;
                    s.head.dir = Direction.RIGHT;
                    break;
                case UP:
                    if(s.head.dir==Direction.DOWN)
                        break;
                    s.head.dir = Direction.UP;
                    break;
                case DOWN:
                    if(s.head.dir==Direction.UP)
                        break;
                    s.head.dir = Direction.DOWN;
                    break;
            }});
    }

        @Override
        public void start(Stage primaryStage) throws Exception{
            Pane root = new Pane();
            Scene scene = new Scene(root, COL*BLOCK_SIZE, ROW*BLOCK_SIZE);
            initKeyListener(scene);
            new Thread( new PaintThread()).start();
            root.getChildren().add(canvas);
            canvas.relocate(0,0);
            paint(canvas);
            primaryStage.setTitle("EatSnake");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
        public void paintOutWall(Canvas canvas){
            GraphicsContext gc = canvas.getGraphicsContext2D();
            gc.setFill(Color.BLACK);
            for (int i = 0; i < COL; i++) {
                gc.fillRect(i*BLOCK_SIZE,0,BLOCK_SIZE,BLOCK_SIZE);
            }

            for (int i = 0; i < ROW; i++) {
                gc.fillRect(0,i*BLOCK_SIZE,BLOCK_SIZE,BLOCK_SIZE);
            }
            for (int i = 0; i < ROW; i++) {
                gc.fillRect((COL-1)*BLOCK_SIZE,i*BLOCK_SIZE,BLOCK_SIZE,BLOCK_SIZE);
            }
            for (int i = 0; i < COL; i++) {
                gc.fillRect(i*BLOCK_SIZE,(ROW-1)*BLOCK_SIZE,BLOCK_SIZE,BLOCK_SIZE);
            }

        }
        private void paintInWall(Canvas canvas) {
            GraphicsContext gc = canvas.getGraphicsContext2D();
            gc.setFill(Color.BLACK);
            for (int i = COL/3; i < 2*COL/3; i++) {
                gc.fillRect(i*BLOCK_SIZE,ROW*BLOCK_SIZE/3/10*10,BLOCK_SIZE,BLOCK_SIZE);
            }

            for (int i = 0; i < ROW; i++) {
                gc.fillRect(0,i*BLOCK_SIZE,BLOCK_SIZE,BLOCK_SIZE);
            }
            for (int i = 0; i < ROW; i++) {
                gc.fillRect((COL-1)*BLOCK_SIZE,i*BLOCK_SIZE,BLOCK_SIZE,BLOCK_SIZE);
            }
            for (int i = COL/3; i < 2*COL/3; i++) {
                gc.fillRect(i*BLOCK_SIZE,2*ROW*BLOCK_SIZE/3/10*10,BLOCK_SIZE,BLOCK_SIZE);
            }


        }

        public void paint(Canvas canvas) throws InterruptedException {
                GraphicsContext gc = canvas.getGraphicsContext2D();
                gc.setFill(Color.GRAY);
                gc.fillRect(0, 0, COL * BLOCK_SIZE, ROW * BLOCK_SIZE);
                for (int i = 1; i < COL-1; i++) {
                    gc.strokeLine(i * BLOCK_SIZE, 0, i * BLOCK_SIZE, ROW * BLOCK_SIZE);
                }
                for (int i = 1; i < ROW-1; i++) {
                    gc.strokeLine(0, i * BLOCK_SIZE, COL * BLOCK_SIZE, i * BLOCK_SIZE);
                }
                paintOutWall(canvas);
                paintInWall(canvas);


                gc.setFill(Color.YELLOW);
                gc.fillText("得分："+y.score,20,60);
                if(y.flag==false){
                    gc.setFont(Font.font("宋体",80));
                   Font.font(80);
                    gc.fillText("游戏结束",150,200,250);
                }
                else {
                    //if(score%10==0&&score!=0)
                    m.draw(canvas);
                    f.draw(canvas);
                    s.draw(canvas);
                    s.eat(f);
                    s.eatMagic(m);
                }
        }



    public class PaintThread implements Runnable{
            public void run(){
                while(y.flag) {
                    try {
                        paint(canvas);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                while(!y.flag){
                    try {
                        paint(canvas);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        public static void main(String[] args) {
            launch(args);
        }
    }

